package com.yeskery.boot.log.record.starter.parser;

import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义函数和 SpEL 解析类
 * @author dushuangjiang
 * @date 2021-11-11 17:26
 */
public class LogRecordExpressionEvaluator extends CachedExpressionEvaluator {

    private final Map<ExpressionKey, Expression> expressionCache = new ConcurrentHashMap<>(64);

    /**
     * 解析SpEL表达式
     * @param conditionExpression 条件表达式
     * @param methodKey 注解key
     * @param evalContext SpEL上下文对象
     * @return 解析后的SpEL值
     */
    public String parseExpression(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
        return getExpression(this.expressionCache, methodKey, conditionExpression).getValue(evalContext, String.class);
    }
}
